﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Diagnostics;
using System.Globalization;

namespace AllTalkProxy
{
    public class Jid
    {
        /*megha@gmail.com/something*/
        public string UserName { get; private set; } /*megha*/
        public string Domain { get; private set; } /*gmail*/
        public string UserNameDomain { get; private set; }
        public string ResourceName { get; private set; } /*something*/
        public string Value { get; private set; }
        public Presence Presence { get; private set; }
        public int Priority { get; private set; }

        public Jid(string jid)
        {
            this.Value = jid;
          
            int ampersandPosition = jid.IndexOf('@');
            this.UserName = jid.Substring(0, ampersandPosition);

            int slashPosition = jid.IndexOf('/', ampersandPosition);
            if (slashPosition < 0)
            {
                slashPosition = jid.Length;
            }
            this.Domain = jid.Substring(ampersandPosition + 1, slashPosition - ampersandPosition - 1);
            this.UserNameDomain = String.Concat(this.UserName, this.Domain, CultureInfo.InvariantCulture);

            if (slashPosition != jid.Length)
            {
                this.ResourceName = jid.Substring(slashPosition + 1, jid.Length - slashPosition - 1);
            }      
        }

        internal void UpdatePresence(XElement presenceXml)
        {
            XElement showElement = presenceXml.Descendants("show").FirstOrDefault();
            if (showElement != null)
            {
                Presence presence;
                string value = showElement.Value;
                if (!Enum.TryParse(value, /*ignoreCase*/ true, out presence))
                {
                    Debug.Fail("Unexpected show value");
                }
                this.Presence = presence;
            }
            else
            {
                this.Presence = Presence.Chat;
            }
            XElement priorityElement = presenceXml.Descendants("priority").FirstOrDefault();
            int priority = 0;
            if (priorityElement != null)
            {
                priority = Convert.ToInt32(priorityElement.Value);
                this.Priority = priority;
            }
        }

        public override string ToString()
        {
            return this.Value;
        }

        public override bool Equals(object obj)
        {
            Jid otherJid = obj as Jid;
            if (otherJid != null)
            {
                return String.Equals(this.UserNameDomain, otherJid.UserNameDomain, StringComparison.OrdinalIgnoreCase)
                    && String.Equals(this.ResourceName, otherJid.ResourceName, StringComparison.OrdinalIgnoreCase);
            }
            return false;
        }

        public override int GetHashCode()
        {
            if (String.IsNullOrEmpty(this.ResourceName))
            {
                return this.UserNameDomain.GetHashCode();
            }
            return this.UserNameDomain.GetHashCode() ^ this.ResourceName.GetHashCode();
        }
    }
}
